<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 作业1：
        // 写一个函数，格式化时间 formatDate(), 参数可以是字符串，也可以是时间对象。
        // 效果： 2021 - 10 - 05 15：15：09 星期几
        let t = new Date();
        function formatDate(time) {
            let timer = new Date(time);
            let year = timer.getFullYear();
            let month = timer.getMonth() + 1;
            let date = timer.getDate();
            let day = timer.getDay();
            let hour = timer.getHours();
            let minutes = timer.getMinutes();
            let second = timer.getSeconds();
            if (month < 10) {
                month = "0" + month;
            }
            if (date < 10) {
                date = "0" + date;
            }
            if (hour < 10) {
                hour = "0" + hour;
            }
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            if (second < 10) {
                second = "0" + second;
            }
            if (day == 0) {
                day = '星期天';
            }
            if (day == 1) {
                day = '星期一';
            }
            if (day == 2) {
                day = '星期二';
            }
            if (day == 3) {
                day = '星期三';
            }
            if (day == 4) {
                day = '星期四';
            }
            if (day == 5) {
                day = '星期五';
            }
            if (day == 6) {
                day = '星期六';
            }
            return `${year}-${month}-${date} ${hour}:${minutes}:${second} ${day}`;

        }

        document.write(formatDate(t));
    </script>
</body>

</html>